#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>

using std::cin;
using std::cout;
using std::vector;
using std::endl;

template<typename T>
void Input(vector<vector<T>> & table, vector<vector<T>> &cost) {
    size_t n, m;
    cin >> n >> m;
    table.resize(n), cost.resize(n);
    for (size_t i = 0; i < n; ++i) {
        for (size_t j = 0; j < m; ++j) {
            T next;
            cin >> next;
            table[i].push_back(next);
            cost[i].push_back(0);
        }
    }
    cost[0][0] = table[0][0];
    for (size_t i = 1; i < m; ++i) {
        cost[0][i] = cost[0][i - 1] + table[0][i];
    }
    for (size_t i = 1; i < n; ++i) {
        cost[i][0] = cost[i - 1][0] + table[i][0];
    }
}

template<typename T>
void Solution(const vector<vector<T>> & table, vector<vector<T>> &cost) {
    for (size_t i = 1; i < cost.size(); ++i) {
        for (size_t j = 1; j < cost[i].size(); ++j) {
            cost[i][j] = min(cost[i - 1][j], cost[i][j - 1]) + table[i][j];
        }
    }
    cout << cost[cost.size() - 1][cost[cost.size() - 1].size() - 1];
}

int main() {
    vector<vector<int>> table;
    vector<vector<int>> cost;
    Input(table, cost);
    Solution(table, cost);
}